package com.corpgovernment.permission.mapper;


import java.util.List;

import com.corpgovernment.permission.entity.db.Menu;
import org.apache.ibatis.annotations.*;

/**
 *
 *
 * @author marco.zheng   (cq_zheng@trip.com)
 * @date 2023/8/11
 */
@Mapper
public interface MenuMapper {

    @Select("select * from menu where is_deleted = false and valid='1' and `url`=#{url}")
    Menu getMenuByUrl(@Param("url") String url);
    @Select("select * from menu where is_deleted = false and id=#{id}")
    Menu getMenuById(@Param("id") Long id);
    @Select("select * from menu where is_deleted = false and valid='1'")
    List<Menu> getMenuList();

    @Update("update menu set is_deleted = true where id=#{id}")
    int deleteById(@Param("id") Long id);
    // todo
    @Update("update menu set is_deleted = true where id=#{id}")
    int update(Menu menuDao);

    @Update("<script> update menu set permission = #{permission} <if test='name != null'> , name = #{name} </if> " +
            "<if test='parentId != null'> , parent_id = #{parentId} </if>"+
            "<if test='isParentMenu != null'> , is_parent_menu = #{isParentMenu} </if>"+
            "<if test='icon != null'> , icon = #{icon} </if>"+
            "<if test='sort != sort'> , sort = #{sort} </if>"+
            "<if test='type != null'> , `type` = #{type} </if>"+
            "<if test='key != null'> , `key` = #{key} </if>"+
            "<if test='url != null'> , url = #{url} </if>"+
            "<if test='dataPermission != null'> , data_permission = #{dataPermission} </if>"+
            "<if test='valid != null'> , valid = #{valid} </if>"+
            "<if test='isDeleted != null'> , is_deleted = #{isDeleted} </if>"+
            "<if test='serviceUrl != null'> , service_url = #{serviceUrl} </if>"+
            "where id=#{id} </script>")
    int updateById(Menu menuDao);

    @Insert("<script>insert into menu  (name,parent_id,is_parent_menu,icon,sort,`type`,permission,service_url,is_deleted,`key`,url,valid,data_permission)  values " +
            "<foreach collection='menuDaoList' item='item' index='index' separator=','>"+
            "(#{item.name},#{item.parentId},#{item.isParentMenu},#{item.icon},#{item.sort},#{item.type},#{item.permission}," +
            "#{item.serviceUrl},#{item.isDeleted},#{item.key},#{item.url},#{item.valid},#{item.dataPermission})</foreach>" +
            "</script>")
    int insertBatch(@Param("menuDaoList") List<Menu> menuDaoList);

    @Select("<script>select * from menu where is_deleted = false and id in " +
            "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>"
    )
    List<Menu> getMenuByIds(@Param("ids")List<Long> ids);
}
